﻿using System.Linq;
using System.Text;
using System.Collections;
using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.DictionaryActions.Add
{
	/// <summary>
	/// 添加JSON对象的Handler
	/// </summary>
	public class AddJSONObjectActionHandler : DictionaryAddActionHandler
	{
		public override void DoRender( StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			var sbValue = new StringBuilder();
			base.DoRenderChildNodes( sbValue, rootParamObject, parentParamObject, context );
			var json = sbValue.ToString().Trim();
			var jsonObject = json.IsNullOrEmpty() ? new Hashtable() : json.ToDictionary();
			var dict = context.ToDictionary( DictionaryActionHandler.DICTIONARY_OBJECT );
			var property = this.Element.Parent.Handler.GetPropertyName( rootParamObject, parentParamObject, context );
			dict[ property ] = jsonObject;
		}
	}
}
